var jsonObj; 
var appKey = "4238391933";
var appSecret = "be4dc3c26087df015f70c1fd860186eb";
// var redirectUrl = "http://hellonima.sinaapp.com/callback.php";
var redirectUrl = "http://hellonima.sinaapp.com/jsonformat.php";
var mainCode;
function init() {
    document.addEventListener("deviceready", loadConfig_grant, true);
    /*$(document).ready(function(){
     loadRssChannelTemp();
     });*/
}
function loadConfig_grant() {
	document.addEventListener("backbutton", function() {
                              window.location.href = "info.html";
                              }, true);
	$.mobile.showPageLoadingMsg("a", "Loading...", true);
	var theme = localStorage.theme;
    changeTheme(theme);
	mainCode=localStorage.mainCode;
	if(!mainCode){
		// OAUTH授权页面
		var url = "https://api.weibo.com/oauth2/authorize?response_type=code&"
        + "client_id=" + appKey + "&redirect_uri=" + redirectUrl
        + "&display=mobile";
        
		// iframe中显示新浪微博授权页面
		$("#grant").attr("src", url);
		window.onmessage = function(e) {    
			var json_str=e.data;  
			var json_arr=json_str.split("-");
			mainCode=json_arr[0];
			if(mainCode!="http://hellonima.sinaapp.com/jsonformat.php"){
				localStorage.setItem("mainCode",mainCode);
				localStorage.setItem("uid",json_arr[1]);
				showPostWeiboPage();			
			}else{
				window.location.href="info.html";
			}
			// $("#grant").attr("src",postUrl);
		}; 
	}else{
		showPostWeiboPage();
	}
	$.mobile.hidePageLoadingMsg();
}

// 在grant.html页面中展示位发微薄的页面
function showPostWeiboPage(){
	var postUrlStr=localStorage.url.replace(/http:\/\/[\s\S]*src=/,"");
	var postUrl= decodeURIComponent(postUrlStr);
	postUrl="http://"+postUrl+" ";
	var areaContent=postUrl+" (分享新闻)";
	$("#textarea").attr("value",areaContent);
	var uid=localStorage.uid;
	getMyUserInfo(mainCode,uid); 
	$("#grant").attr("src","http://hellonima.sinaapp.com/weibolist.php?access_token="+mainCode);
	$('#grant').css({'width' : $(window).width()}); 
	$("#postArea").css("display","block");
}

function logoutWeibo(){
	localStorage.setItem("mainCode","");
	localStorage.setItem("uid","");
	window.location.href="info.html";
}
function getMyUserInfo(code,uid){
	$.ajax({
           type:"GET",
           url:"https://api.weibo.com/2/users/show.json",
           data:"access_token="+code+"&uid="+uid,
           success:function(msg){
           //alert(msg);
           $("#userInfo").append(msg.name+",您好！");
           },
           error:function(){
           $("#userInfo").append(uid);;
           }
           });
}

//在grant.html页面，点击文本框下的发送按钮，发送新浪微博
function postWeibo(){
	var word=$("#textarea").attr("value");
	word=encodeURI(word);
	$.ajax({
		   type:"POST",
		   url:"https://api.weibo.com/2/statuses/update.json",
		   data:"access_token="+mainCode+"&status="+word,
		   success:function(msg){
           $("textarea").attr("value","");
           $("#grant").attr("src","http://hellonima.sinaapp.com/weibolist.php?access_token="+mainCode);
           navigator.notification.confirm('微博发送成功！', // message
                                          function(button){
                                          }	, // callback function
                                          '提示', // title
                                          '确定' // confirm 选项，用逗号隔开
                                          );   
		   },
		   error:function(){
           navigator.notification.confirm('微博发送失败！\n可能是如下原因所致：\n1.超过会话时间，请重新登录\n2.与上次所发博文内容重复\n3.网络连接不畅', // message
                                          function(button){
                                          }	, // callback function
                                          '警告', // title
                                          '确定' // confirm 选项，用逗号隔开
                                          );
		   }    
           });  	 
}
//在grant.html页面，点击文本框下的重写按钮，重写文本框里的内容
function clearTextArea(){
	navigator.notification.confirm('确定要清空文本框中内容？', // message
                                   function(button){
                                   if(button===1){
                                   $("textarea").attr("value","");
                                   }
                                   }	, // callback function
                                   '警告', // title
                                   '确定,取消' // confirm 选项，用逗号隔开
                                   );
}